//
//  UIViewController+HUD.m
//  HtglEducation
//
//  Created by lizhenning on 15/4/21.
//  Copyright (c) 2015年 lizhenning. All rights reserved.
//

#import "UIViewController+HUD.h"
#import "MBProgressHUD.h"
#import <objc/runtime.h>
#import "Macros.h"
#import "RTSpinKitView.h"




static const void *HttpRequestSKVKey = &HttpRequestSKVKey;

static const void *HttpRequestHUDKey = &HttpRequestHUDKey;

static const void *HttpRequestSKVBgKey = &HttpRequestSKVBgKey;

@implementation UIViewController (HUD)

- (UIView *)bgSKV
{
    return objc_getAssociatedObject(self, HttpRequestSKVBgKey);
}

- (RTSpinKitView *)SKV
{
    return objc_getAssociatedObject(self, HttpRequestSKVKey);
}

- (void)setSKVBg:(UIView *)view
{
    objc_setAssociatedObject(self, HttpRequestSKVBgKey, view, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)setSKV:(RTSpinKitView *)SKV
{
    objc_setAssociatedObject(self, HttpRequestSKVKey, SKV, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)startSKV
{
    UIView *view = [[UIView alloc] init];
    [view setBackgroundColor:[UIColor whiteColor]];
    [view setUserInteractionEnabled:YES];
    [self setSKVBg:view];
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    
    RTSpinKitView *skv = [[RTSpinKitView alloc] initWithStyle:RTSpinKitViewStyleArc color:HexRGBA(0x4f5660, 1) spinnerSize:30];
    [skv setCenter:CGPointMake(kScreenWidth/2.0, kScreenHeight/2.0-32)];
    [view addSubview:skv];
    [skv startAnimating];
    
}



- (void)stopSKV
{
    [[self SKV] stopAnimating];
    
    [[self SKV] removeFromSuperview];
        
    [[self bgSKV] removeFromSuperview];

}

#pragma mark

- (MBProgressHUD *)HUD{
    return objc_getAssociatedObject(self, HttpRequestHUDKey);
}

- (void)setHUD:(MBProgressHUD *)HUD{
    objc_setAssociatedObject(self, HttpRequestHUDKey, HUD, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)showHudInView:(UIView *)view hint:(NSString *)hint{
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:view];
    HUD.label.text = hint;
    HUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    //设置等待框背景色为黑色
    HUD.bezelView.backgroundColor = [UIColor blackColor];
    HUD.removeFromSuperViewOnHide = YES;
    [UIActivityIndicatorView appearanceWhenContainedIn:[MBProgressHUD class], nil].color = [UIColor whiteColor];
    [view addSubview:HUD];
    [HUD showAnimated:YES];
    [self setHUD:HUD];
}

- (void)showHudInView:(UIView *)view hint:(NSString *)hint yOffset:(float)yOffset{
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:view];
    //    HUD.labelText = hint;
    HUD.label.text=hint;
    [view addSubview:HUD];
    [HUD showAnimated:YES];
     HUD.yOffset += yOffset;
    HUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    //设置等待框背景色为黑色
    HUD.bezelView.backgroundColor = [UIColor blackColor];
    HUD.removeFromSuperViewOnHide = YES;
    [UIActivityIndicatorView appearanceWhenContainedIn:[MBProgressHUD class], nil].color = [UIColor whiteColor];
    [self setHUD:HUD];
}

- (void)showHint:(NSString *)hint
{
    //显示提示信息
    UIView *view = [[UIApplication sharedApplication].delegate window];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.userInteractionEnabled = NO;
    // Configure for text only and offset down
    hud.mode = MBProgressHUDModeText;
    hud.label.text = hint;
    hud.label.backgroundColor = [UIColor clearColor];
    hud.label.textColor = [UIColor whiteColor];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [UIColor blackColor];
    hud.margin = 10.f;
    hud.yOffset = 200.f  * kAutoScanleX;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:2];
}

- (void)showHint:(NSString *)hint yOffset:(float)yOffset {
    //显示提示信息
    UIView *view = [[UIApplication sharedApplication].delegate window];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.userInteractionEnabled = NO;
    // Configure for text only and offset down
    hud.mode = MBProgressHUDModeText;
    hud.label.text = hint;
    hud.label.backgroundColor = [UIColor clearColor];
    hud.label.textColor = [UIColor whiteColor];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [UIColor blackColor];
    hud.margin = 10.f;
    hud.yOffset = 200.f  * kAutoScanleX;
    [hud hide:YES afterDelay:2];
}

- (void)hideHud{
    [[self HUD] hideAnimated:YES];
}


@end
